package vn.hocweb.dao.dao_implement;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import vn.hocweb.dao.dao_interface.INguoiDungDAO;
import vn.hocweb.entity.NguoiDung;

public class NguoiDungDAO extends GenericDAO implements INguoiDungDAO{

	@Override
	public NguoiDung findByID(String id) {
		Connection connection = getConnection();
		NguoiDung result = null;
		String sql = "select * from nguoidung where tendangnhap = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setString(1, id);
			ResultSet effected = pre.executeQuery();
			if (effected.next()){
				String tenDangNhap = effected.getString("tendangnhap");
				
				String matKhau = effected.getString("matkhau");
				String quyen = effected.getString("quyen");
				Date ngayDangKy = effected.getDate("ngaydangky");
				String email = effected.getString("email");
				String dienThoai = effected.getString("dienthoai");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				result = new NguoiDung(thuTu, noiBat, hienThi, 
						tenDangNhap, matKhau, quyen, ngayDangKy, email, dienThoai);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<NguoiDung> findAll(int firstItem, int batchSize) {
		Connection connection = getConnection();
		List<NguoiDung> result = new ArrayList<NguoiDung>();
		String sql = "select * from nguoidung limit ?, ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, firstItem);
			pre.setLong(2, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				String tenDangNhap = effected.getString("tendangnhap");
				
				String matKhau = effected.getString("matkhau");
				String quyen = effected.getString("quyen");
				Date ngayDangKy = effected.getDate("ngaydangky");
				String email = effected.getString("email");
				String dienThoai = effected.getString("dienthoai");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				NguoiDung row = new NguoiDung(thuTu, noiBat, hienThi, 
						tenDangNhap, matKhau, quyen, ngayDangKy, email, dienThoai);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public int size() {
		Connection connection = getConnection();
		int result = 0;
		String sql = "select count(*) from nguoidung";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			ResultSet effected = pre.executeQuery();
			if(effected.next()){
				result = effected.getInt(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean save(NguoiDung entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "insert into nguoidung (tendangnhap, matkhau, quyen, " +
				"thutu, noibat, hienthi, ngaydangky, email, dienthoai) " +
				"values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setString(1, entity.getTenDangNhap());
			pre.setString(2, entity.getMatKhau());
			pre.setString(3, entity.getQuyen());
			pre.setInt(4, entity.getThuTu());
			pre.setBoolean(5, entity.isNoiBat());
			pre.setBoolean(6, entity.isHienThi());
			pre.setDate(7, entity.getNgayDangKy());
			pre.setString(8, entity.getEmail());
			pre.setString(9, entity.getDienThoai());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean delete(NguoiDung entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "delete from nguoidung where tendangnhap = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setString(1, entity.getTenDangNhap());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean remove(NguoiDung entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update nguoidung set hienthi = ? where tendangnhap = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, false);
			pre.setString(2, entity.getTenDangNhap());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean restore(NguoiDung entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update nguoidung set hienthi = ? where tendangnhap = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, true);
			pre.setString(2, entity.getTenDangNhap());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

}
